package com.lgr.store.controller;

import com.lgr.store.service.SupplierService;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.ContextUtils;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.EmpVo;
import com.lgr.store.vo.SupplierVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/supplier")
public class SupplierController {
    @Resource
    SupplierService supplierService;
    @Resource
    HttpSession session;

    @RequestMapping(value="/json")
    @ResponseBody
    public Map json(SupplierVo supplier,HttpServletResponse response)throws IOException {
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();
        SupplierVo supplierList= supplierService.getById(supplier.getSupplierId());
//        JSONObject object = new JSONObject();
//        object.put("supplierList", supplierList);
//        out.print(ContextUtils.getJSON(object));
//        out.flush();
//        out.close();
//        return null;

        Map map = new HashMap();
        map.put("supplierList",supplierList);
        return map;
    }
    //新增
    @RequestMapping("/init")
    @ResponseBody
    public Map add(HttpServletRequest request,SupplierVo supplier){
        //系统日志
        if(supplier==null){
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(1,supplier));
        }else{
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,supplier));
        }
        supplierService.saveOrUpdate(supplier);
        Map<String,Object> map = new HashMap<String, Object>();
        return map;
    }
    //删除
    @RequestMapping("/supplierdel")
    @ResponseBody
    public Map del(HttpServletRequest request,SupplierVo supplier){
        String supplierId = request.getParameter("supplierId");
        //系统日志
        EmpVo emp = (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(3,supplier));

        supplierService.supplierDel(Integer.parseInt(supplierId));
        Map<String,Object> maps = new HashMap<String, Object>();
        return maps;
    }
    //查询
    @RequestMapping("/supplierlist")
    public String list(SupplierVo supplier, PageObject pager, ModelMap map){
        int cut = 0;
        try{
            cut = supplierService.supplierCount();
            pager.setTotalRows(cut);
        }catch (Exception e){e.printStackTrace();}

        List<SupplierVo> supplierList = supplierService.list(supplier,pager);

        map.addAttribute("supplier",supplier);
        map.addAttribute("supplierList",supplierList);
        map.addAttribute("pager",pager);
        return "supplierList";
    }
}
